{% extends "inventory/base.html" %}
{% load crispy_forms_tags %}

{% block title %}操作日志报表{% endblock %}

{% block extra_head %}
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
{% endblock %}

{% block content %}
<div class="container">
    <div class="row mb-4">
        <div class="col-md-8">
            <h1 class="h2">操作日志报表</h1>
            <p class="text-muted">
                日期范围: {{ start_date|date:"Y-m-d" }} 至 {{ end_date|date:"Y-m-d" }}
            </p>
        </div>
        <div class="col-md-4 text-md-end">
            <a href="{% url 'reports_index' %}" class="btn btn-outline-secondary">
                <i class="bi bi-arrow-left me-1"></i> 返回报表中心
            </a>
        </div>
    </div>

    <!-- 搜索表单 -->
    <div class="row mb-4">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header">
                    <h3 class="h5 mb-0">设置报表参数</h3>
                </div>
                <div class="card-body">
                    <form method="post">
                        {% csrf_token %}
                        <div class="row">
                            <div class="col-md-5">
                                {{ form.start_date|as_crispy_field }}
                            </div>
                            <div class="col-md-5">
                                {{ form.end_date|as_crispy_field }}
                            </div>
                            <div class="col-md-2 d-flex align-items-end">
                                <button type="submit" class="btn btn-primary w-100">查询</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <!-- 图表 -->
    <div class="row mb-4">
        <div class="col-md-6">
            <div class="card h-100">
                <div class="card-header">
                    <h3 class="h5 mb-0">操作类型统计</h3>
                </div>
                <div class="card-body">
                    <canvas id="operationTypeChart" height="250"></canvas>
                </div>
            </div>
        </div>
        <div class="col-md-6">
            <div class="card h-100">
                <div class="card-header">
                    <h3 class="h5 mb-0">操作员统计</h3>
                </div>
                <div class="card-body">
                    <canvas id="operatorChart" height="250"></canvas>
                </div>
            </div>
        </div>
    </div>

    <!-- 操作日志列表 -->
    <div class="row">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h3 class="h5 mb-0">操作日志列表</h3>
                    <span class="badge bg-info">{{ log_data.logs|length }} 条记录</span>
                </div>
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table table-hover mb-0">
                            <thead class="table-light">
                                <tr>
                                    <th>操作时间</th>
                                    <th>操作员</th>
                                    <th>操作类型</th>
                                    <th>关联对象</th>
                                    <th>详情</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for log in log_data.logs %}
                                <tr>
                                    <td>{{ log.timestamp|date:"Y-m-d H:i:s" }}</td>
                                    <td>{{ log.operator.username }}</td>
                                    <td>
                                        {% if log.operation_type == 'INVENTORY' %}
                                        <span class="badge bg-primary">库存操作</span>
                                        {% elif log.operation_type == 'SALE' %}
                                        <span class="badge bg-success">销售操作</span>
                                        {% elif log.operation_type == 'MEMBER' %}
                                        <span class="badge bg-info">会员操作</span>
                                        {% elif log.operation_type == 'INVENTORY_CHECK' %}
                                        <span class="badge bg-warning">库存盘点</span>
                                        {% else %}
                                        <span class="badge bg-secondary">其他操作</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        {{ log.related_content_type.name }} #{{ log.related_object_id }}
                                    </td>
                                    <td>
                                        <button type="button" class="btn btn-sm btn-outline-info" 
                                                data-bs-toggle="tooltip" data-bs-placement="left" 
                                                title="{{ log.details }}">
                                            <i class="bi bi-info-circle"></i>
                                        </button>
                                    </td>
                                </tr>
                                {% empty %}
                                <tr>
                                    <td colspan="5" class="text-center py-4">没有操作日志数据</td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

{% if log_data.operation_type_stats %}
<script>
    document.addEventListener('DOMContentLoaded', function() {
        // 操作类型统计图表
        const ctxType = document.getElementById('operationTypeChart').getContext('2d');
        
        // 准备数据
        const typeLabels = [
            {% for type_stat in log_data.operation_type_stats %}
                '{{ type_stat.operation_type }}',
            {% endfor %}
        ];
        
        const typeCounts = [
            {% for type_stat in log_data.operation_type_stats %}
                {{ type_stat.count }},
            {% endfor %}
        ];
        
        // 创建图表
        new Chart(ctxType, {
            type: 'pie',
            data: {
                labels: typeLabels,
                datasets: [{
                    data: typeCounts,
                    backgroundColor: [
                        'rgba(13, 110, 253, 0.7)',
                        'rgba(25, 135, 84, 0.7)',
                        'rgba(13, 202, 240, 0.7)',
                        'rgba(255, 193, 7, 0.7)',
                        'rgba(108, 117, 125, 0.7)'
                    ],
                    borderWidth: 1
                }]
            },
            options: {
                responsive: true,
                plugins: {
                    legend: {
                        position: 'right'
                    },
                    title: {
                        display: false
                    }
                }
            }
        });
        
        // 操作员统计图表
        const ctxOp = document.getElementById('operatorChart').getContext('2d');
        
        // 准备数据
        const opLabels = [
            {% for op_stat in log_data.operator_stats %}
                '{{ op_stat.operator__username }}',
            {% endfor %}
        ];
        
        const opCounts = [
            {% for op_stat in log_data.operator_stats %}
                {{ op_stat.count }},
            {% endfor %}
        ];
        
        // 创建图表
        new Chart(ctxOp, {
            type: 'bar',
            data: {
                labels: opLabels,
                datasets: [{
                    label: '操作次数',
                    data: opCounts,
                    backgroundColor: 'rgba(13, 202, 240, 0.7)',
                    borderColor: 'rgba(13, 202, 240, 1)',
                    borderWidth: 1
                }]
            },
            options: {
                indexAxis: 'y',
                responsive: true,
                plugins: {
                    legend: {
                        display: false
                    }
                },
                scales: {
                    x: {
                        beginAtZero: true,
                        ticks: {
                            precision: 0
                        }
                    }
                }
            }
        });
        
        // 初始化工具提示
        const tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
        tooltipTriggerList.map(function(tooltipTriggerEl) {
            return new bootstrap.Tooltip(tooltipTriggerEl);
        });
    });
</script>
{% endif %}
{% endblock %} 